﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.SystemManage
{
    public partial class DBSubjectSearchWindow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public Store GridStore { get; set; }
        public static Action<PageEntity> SearchFun;

        public void Show(Action<PageEntity> _SearchFun)
        {
            SearchFun = _SearchFun;
            this.DBSubjectSearch.Show();
        }

        protected void SearchDBSubject(object sender, DirectEventArgs e)
        {
            StringBuilder sqlWhere = new StringBuilder(" Status<>0 ");
            if (!string.IsNullOrWhiteSpace(Knowledge.Text))
            {
                var knowledge = Knowledge.Text.Trim();
                knowledge = knowledge.SqlSafe();
                sqlWhere.Append(string.Format(" AND Knowledge like '%{0}%'", knowledge));
            }
            if (!string.IsNullOrWhiteSpace(KeyWord.Text))
            {
                var keyword = KeyWord.Text.Trim();
                keyword = keyword.SqlSafe();
                sqlWhere.Append(string.Format(" AND KeyWord like '%{0}%'", keyword));
            }
            if (!string.IsNullOrWhiteSpace(Title.Text))
            {
                var title = Title.Text.Trim();
                title = title.SqlSafe();
                sqlWhere.Append(string.Format(" AND Title like '%{0}%'", title));
            }
            if (!string.IsNullOrWhiteSpace(Content.Text))
            {
                var content = Content.Text.Trim();
                content = content.SqlSafe();
                sqlWhere.Append(string.Format(" AND Content like '%{0}%'", content));
            }
            if (!string.IsNullOrWhiteSpace(Stage.Text))
            {
                var stage = Stage.Text.Trim();
                stage = stage.SqlSafe();
                sqlWhere.Append(string.Format(" AND Stage='{0}'", stage));
            }
            if (!string.IsNullOrWhiteSpace(SubjectType.Text))
            {
                var subjecttype = SubjectType.Text.Trim();
                subjecttype = subjecttype.SqlSafe();
                sqlWhere.Append(string.Format(" AND SubjectType='{0}'", subjecttype));
            }
            if (!string.IsNullOrWhiteSpace(Challenge.Text))
            {
                var challenge = Challenge.Text.Trim();
                challenge = challenge.SqlSafe();
                sqlWhere.Append(string.Format(" AND Challenge='{0}'", challenge));
            }
            if (RadioYes.Checked)
                sqlWhere.Append(" and Lock=1");
            else if (RadioNo.Checked)
            {
                sqlWhere.Append(" and Lock=0");
            }

            PageEntity pageEntity = new PageEntity();
            pageEntity.SqlWhere = sqlWhere.ToString();
            SearchFun.Invoke(pageEntity);
            GridStore.Reload();
            DBSubjectSearch.Hide();
        }
    }
}